package com.holyn.guofang.view.home;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.model.webhttp.wo.UpdateInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.view.BaseFragment;
import com.holyn.guofang.view.my.MyCardManagerActivity;
import com.holyn.guofang.view.my.MyChangeDeviceActivity;
import com.holyn.guofang.view.my.MyDealProcessActivity;
import com.holyn.guofang.view.my.MyLogActivity;
import com.holyn.guofang.view.my.MyOrderActivity;
import com.holyn.guofang.view.my.MyRechargeActivity;
import com.holyn.guofang.view.my.MyRegisterInfoActivity;
import com.holyn.guofang.view.my.MyRegisterInfoSecondaryDeviceListActivity;
import com.holyn.guofang.view.my.MyUpdatePasswordActivity;
import com.holyn.guofang.viewcommon.dialog.ToastDialog;
import com.holyn.guofang.viewcommon.dialog.UpdateAppDialog2;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/6/13.
 */

public class HomeMyFragment extends BaseFragment implements BaseView, MyContract.HomeListenenr{
    private static final int REQUEST_ACTION_POST_LOGOUT= 0x0001;
    private static final int REQUEST_ACTION_UPDATE_CHECK = 0x0002;

    @BindView(R.id.tv_welcome)
    TextView tvWelcome;
    @BindView(R.id.ll_my_process)
    LinearLayout llMyProcess;
    @BindView(R.id.ll_my_info)
    LinearLayout llMyInfo;
    @BindView(R.id.ll_my_password)
    LinearLayout llMyPassword;
    @BindView(R.id.ll_my_card_management)
    LinearLayout llMyCardManagement;
    @BindView(R.id.ll_my_recharge)
    LinearLayout llMyRecharge;
    @BindView(R.id.ll_my_order)
    LinearLayout llMyOrder;
    @BindView(R.id.ll_my_log)
    LinearLayout llMyLog;
    @BindView(R.id.ll_my_logout)
    LinearLayout llMyLogout;

    MyContract.Presenter presenter;
    ToastDialog logoutDialog = null;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_home_my;
    }

    @Override
    protected void initData() {
        presenter = new MyPresenter(this);
        presenter.addHomeListenenr(this);
    }

    @Override
    protected void initView() {
        tvWelcome.setText("欢迎 "+ UserInfoSharePreference.getUserName(getActivity()) + "-"
                + UserInfoSharePreference.getDeviceRemark(getActivity()) + " 进入\"我的账号\"！");
    }

    @OnClick({R.id.ll_my_process, R.id.ll_my_info, R.id.ll_my_sub_num_manager, R.id.ll_my_password, R.id.ll_my_card_management
            , R.id.ll_my_recharge, R.id.ll_my_order, R.id.ll_my_log, R.id.ll_my_check_update, R.id.ll_my_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_my_process:
                getActivity().startActivity(new Intent(getActivity(), MyDealProcessActivity.class));
                break;
            case R.id.ll_my_info:
                getActivity().startActivity(new Intent(getActivity(), MyRegisterInfoActivity.class));
                break;
            case R.id.ll_my_sub_num_manager:
                getActivity().startActivity(new Intent(getActivity(), MyRegisterInfoSecondaryDeviceListActivity.class));
                break;
            case R.id.ll_my_password:
                getActivity().startActivity(new Intent(getActivity(), MyUpdatePasswordActivity.class));
                break;
            case R.id.ll_my_card_management:
                getActivity().startActivity(new Intent(getActivity(), MyCardManagerActivity.class));
                break;
            case R.id.ll_my_recharge:
                getActivity().startActivity(new Intent(getActivity(), MyRechargeActivity.class));
                break;
            case R.id.ll_my_order:
                getActivity().startActivity(new Intent(getActivity(), MyOrderActivity.class));
                break;
            case R.id.ll_my_log:
                getActivity().startActivity(new Intent(getActivity(), MyLogActivity.class));
                break;
            case R.id.ll_my_check_update:
                presenter.postPlatformUpdate(REQUEST_ACTION_UPDATE_CHECK);
                break;
            case R.id.ll_my_logout:
                showLogoutDialog();
                break;
        }
    }

    private void showLogoutDialog(){
        if (logoutDialog == null){
            logoutDialog = new ToastDialog(getActivity(), true, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    presenter.postLogout(REQUEST_ACTION_POST_LOGOUT);
                    dialog.dismiss();
                }
            });
            logoutDialog.setTvContent("是否退出当前账号？");
        }
        logoutDialog.show();
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(int requestAction, StatusWo statusWo) {
        if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            toastMsg("注销登录成功");
            getActivity().finish();
        } else {
            toastMsg("注销登录失败");
        }
    }

    @Override
    public void responseSuccessGetUpdateInfoWo(Integer requestAction, UpdateInfoWo updateInfoWo) {
        if (updateInfoWo.getStatus() == HttpConfigure.STATUS_SUCCESS) {
            if (updateInfoWo.getNeedUpdate()) {
                UpdateAppDialog2 updateAppDialog = new UpdateAppDialog2(Objects.requireNonNull(HomeMyFragment.this.getContext()));
                updateAppDialog.setTvVersion(updateInfoWo.getUpdateInfo().getVersion()+"");
                updateAppDialog.setTvContent(updateInfoWo.getUpdateInfo().getUpdateInformation());
                updateAppDialog.setDownUrl(updateInfoWo.getUpdateInfo().getURL());
                updateAppDialog.show();
            } else {
                toastMsg("已经是最新版本");
            }
        }
    }
}
